const exApp = require('../index.js')
const express = require('express');
const router = express.Router();
const mysql = require("../connect/pomsql")
const { resHandler } = require('../model/resHandler')
// 获取轮播图数据
router.get('/homeData', async (req, res, next) => {
    let sql = `select * from home`;
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
// 喜欢量修改
router.post('/editlikes', async function (req, res) {
    let {likes,id}=req.body
    id=id*1
    let sql = `update home set likes='${likes}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 观看量修改
router.post('/editwatch', async function (req, res) {
    let {watch,id}=req.body
    id=id*1
    let sql = `update home set watch='${watch}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 观看量修改
router.post('/editcheck', async function (req, res) {
    let {watch,id}=req.body
    id=id*1
	let sql = `update lxfxcx set watch='${watch}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 收藏量修改
router.post('/editcollection', async function (req, res) {
    let {collection,id}=req.body
    id=id*1
    let sql = `update home set collection='${collection}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 评论
router.post('/wlkcomment', async function (req, res) {
    let {comment,id}=req.body
    id=id*1
    let sql = `update home set comment='${comment}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 搜索
router.post("/search", async function (req, res) {
  let { search } = req.body;
  console.log(req.body);
  let sql = `SELECT * FROM home WHERE name LIKE '%${search}%'`;
  let data = await mysql(sql);
  res.json({
    code: 1,
    data,
  });
});
// 获取user数据
router.get('/gethistory', async (req, res, next) => {
	let {name} = req.query
    let sql = `select * from user WHERE username='${name}'`;
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
// 历史记录
router.post('/searchHistory', async function (req, res) {
    let {history,id}=req.body
    id=id*1
    let sql = `update user set history='${history}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 获取lxfxcx数据
router.get('/getZp', async (req, res, next) => {
    let sql = `select * from lxfxcx`;
    let result = await mysql(sql)
    resHandler(res,result,'操作失败')
})
// 收藏量修改
router.post('/recommendCollection', async function (req, res) {
    let {collection,id}=req.body
    id=id*1
    let sql = `update lxfxcx set collection='${collection}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
// 评论
router.post('/recommendComment', async function (req, res) {
    let {comment,id}=req.body
    id=id*1
    let sql = `update lxfxcx set comment='${comment}' where id = '${id}'`
    let data = await mysql(sql)
    resHandler(res,data,'操作失败')
})
exApp.use('/', router);